package QC.MessageBox.beans;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by Andaren on 2017/4/7.
 * Used for: 微信传递给第三方服务器的（加密）消息实体
 */
public class WXMessage {

    public static final String MESSAGE_SUCCESS = "success";

    private String toUserName;  // 开发者微信号(原始账号)
    private String fromUserName;// 发送方帐号（一个OpenID）
    private String createTime;  // 消息创建时间 （整型）
    private String msgType;     // 消息类型（测试Text）
    private String msgId;       // 消息ID

    // Text
    private String content;     // 消息内容
    // Event
    private String event;       // 事件通知类型
    private String eventKey;    // 事件KEY值，qrscene_为前缀，后面为二维码的参数值
    private String ticket;      // 二维码的ticket，可用来换取二维码图片
    // Picture
    private String picUrl;      // 图片url地址
    private String mediaId;     // 图片消息媒体id，可以调用多媒体文件下载接口拉取数据。

    private String encrypt;     // 加密之后的消息
    private String msgSignature;// 消息签名

    /**
     * 消息ID计数器
     *      用于生成消息ID
     */
    private static AtomicLong msgIdCount;
    static {
        msgIdCount = new AtomicLong();
        msgIdCount.set(1000L);
    }

    /**
     * 生成返回给微信的消息时，获取消息ID
     * @return
     */
    public static String getMsgIdFromCount() {
        return String.valueOf(msgIdCount.incrementAndGet());
    }

    public WXMessage() {

    }

    public String getToUserName() {
        return toUserName;
    }

    public WXMessage setToUserName(String toUserName) {
        this.toUserName = toUserName;
        return this;
    }

    public String getFromUserName() {
        return fromUserName;
    }

    public WXMessage setFromUserName(String fromUserName) {
        this.fromUserName = fromUserName;
        return this;
    }

    public String getCreateTime() {
        return createTime;
    }

    public WXMessage setCreateTime(String createTime) {
        this.createTime = createTime;
        return this;
    }

    public String getMsgType() {
        return msgType;
    }

    public WXMessage setMsgType(String msgType) {
        this.msgType = msgType;
        return this;
    }

    public String getMsgId() {
        return msgId;
    }

    public WXMessage setMsgId(String msgId) {
        this.msgId = msgId;
        return this;
    }

    public String getContent() {
        return content;
    }

    public WXMessage setContent(String content) {
        this.content = content;
        return this;
    }

    public String getEventKey() {
        return eventKey;
    }

    public WXMessage setEventKey(String eventKey) {
        this.eventKey = eventKey;
        return this;
    }

    public String getTicket() {
        return ticket;
    }

    public WXMessage setTicket(String ticket) {
        this.ticket = ticket;
        return this;
    }

    public String getEncrypt() {
        return encrypt;
    }

    public WXMessage setEncrypt(String encrypt) {
        this.encrypt = encrypt;
        return this;
    }

    public String getMsgSignature() {
        return msgSignature;
    }

    public WXMessage setMsgSignature(String msgSignature) {
        this.msgSignature = msgSignature;
        return this;
    }

    public String getEvent() {
        return event;
    }

    public WXMessage setEvent(String event) {
        this.event = event;
        return this;
    }

    public String getPicUrl() {
        return picUrl;
    }

    public WXMessage setPicUrl(String picUrl) {
        this.picUrl = picUrl;
        return this;
    }

    public String getMediaId() {
        return mediaId;
    }

    public WXMessage setMediaId(String mediaId) {
        this.mediaId = mediaId;
        return this;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("WXMessage{");
        sb.append("toUserName='").append(toUserName).append('\'');
        sb.append(", fromUserName='").append(fromUserName).append('\'');
        sb.append(", createTime='").append(createTime).append('\'');
        sb.append(", msgType='").append(msgType).append('\'');
        sb.append(", msgId='").append(msgId).append('\'');
        sb.append(", content='").append(content).append('\'');
        sb.append(", event='").append(event).append('\'');
        sb.append(", picUrl='").append(picUrl).append('\'');
        sb.append(", mediaId='").append(mediaId).append('\'');
        sb.append(", encrypt='").append(encrypt).append('\'');
        sb.append(", msgSignature='").append(msgSignature).append('\'');
        sb.append('}');
        return sb.toString();
    }

}
